package me.qi.kancha;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.io.file.FileNameUtil;
import cn.hutool.core.util.URLUtil;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.util.ObjectUtils;

import java.util.Arrays;
import java.util.HashMap;
import java.util.UUID;

/**
 * id 加密
 *
 * @author 权限
 * @date 2019/10/9 16:52
 */

public class IDEncode {

    public static void main(String[] args) {
        String id = UUID.randomUUID().toString().replace("-", "");
        String code = new BCryptPasswordEncoder().encode(id);
        System.out.println(id);
        System.out.println(code);
        System.out.println(new HashMap<>().get(null));
        String url = "http://a.com/a/b/c.jpg";
        System.out.println(URLUtil.getPath(url));
        System.out.println(url.substring(0, url.lastIndexOf(".")));
        System.out.println(FileNameUtil.getName(URLUtil.getPath(url)));
        System.out.println(Base64.encode("手动是"));
        System.out.println(new String(Base64.decode("5omL5Yqo5piv")));


        System.out.println(new BCryptPasswordEncoder().encode("123456"));
        System.out.println(companyName("公司总部-集团信息部"));
        System.out.println(companyName("生产系统-甚至公司-城市部"));
        System.out.println(companyName("生产系统-甚至-城市部"));
    }

    private static final String[] COMPANY_KEYS = new String[]{"公司", "总部"};
    /**
     * 从主部门提取公司部门字符串以及公司下一级部门字符串
     *
     * @param mainName
     * @return
     */
    private static String companyName(String mainName) {

        if (!ObjectUtils.isEmpty(mainName)) {
            String[] depts = mainName.split("-");
            for (int i = 0; i < depts.length; i++) {
                for (String key : COMPANY_KEYS) {
                    if (depts[i].indexOf(key) > 0) {
                        String[] strings = Arrays.copyOf(depts, i + 1);
                        return String.join("-", strings);
                    }
                }

            }

        }
        return null;
    }

}
